package com.lizardmind.haofudoctor.ui.studio.gongGao

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.formatToZY
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_gong_gao_input.*
import kotlinx.android.synthetic.main.include_title_right_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.textColor
import org.jetbrains.anko.toast
import org.json.JSONObject

class GongGaoInputActivity : BaseActivity() {
    lateinit var tag:String

    override fun onCreatRootView(): View? {
        setContentView(R.layout.activity_gong_gao_input)
        return null
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        tag=intent.getStringExtra("tag")
        when (tag){
            "gonggao"-> {
                et.setText(DoctorInfo.gonggao)
                if (et.text.isEmpty()){et.hint="请输入公告..."}
                titleText.text="公告"
            }
            "introduction"-> {
                et.setText(DoctorInfo.introduce)
                if (et.text.isEmpty()){et.hint="请输入简介..."}
                titleText.text="简介"
            }
            "good_at"-> {
                et.setText(DoctorInfo.good_at)
                if (et.text.isEmpty()){et.hint="请输入您的擅长..."}
                titleText.text="擅长"
            }
        }
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        tvRight.text="保存"
        tvTextLength.text=et.text.length.toString()+getString(R.string.textLength)
        if (!et.text.isEmpty()){
            tvRight.textColor=ContextCompat.getColor(this@GongGaoInputActivity,R.color.notice_red)
            et.setSelection(et.text.length)
        }
        tvRight.onClick {
            if (!et.text.isEmpty()){
                postMessage()
            }else{
                toast("请输入内容...")
            }
        }

        et.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                tvRight.textColor=if (et.text.isEmpty()) ContextCompat.getColor(this@GongGaoInputActivity,R.color.gray3)
                                    else ContextCompat.getColor(this@GongGaoInputActivity,R.color.notice_red)
                tvTextLength.text=et.text.length.toString()+getString(R.string.textLength)
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })

    }

    private fun postMessage(){
        val json=JSONObject()
        json.put(tag,et.text)
        observe {
            postApi<Request<DoctorBean>>(
                    hashMapOf("action" to "doctor_edit",
                            "token" to DoctorInfo.token,
                            "data" to json.formatToZY()),
                    Consumer {
                        it.dispose {
                            DoctorInfo.introduce=it.introduction
                            DoctorInfo.gonggao=it.gonggao
                            DoctorInfo.good_at=it.good_at

                            this@GongGaoInputActivity.setResult(1)
                            finish()
                        }
                    },context = this@GongGaoInputActivity
            )
        }
    }

}
